//
//  RulesListViewController.m
//  carcareIOS
//
//  Created by wr on 15/7/19.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "RulesListViewController.h"
#import "BaseWebViewController.h"

#define CELL_SECTION_ONE (0 * __MAIN_RATIO_375)

#define CELL_SECTION_TWO (80 * __MAIN_RATIO_375)


@interface RulesListViewController ()<UITableViewDataSource,UITableViewDelegate,RuleDetailViewControllerDelegate>

@property (nonatomic , strong) UITableView * tableView;

@property (nonatomic , strong) NSArray * dataArray;

@property (nonatomic , strong) NSArray * headArray;

@property (nonatomic , copy) NSString * queryIDStr;

@property (nonatomic , strong) ViolationMgr * cityMgr;

@property (nonatomic , strong) ViolationCarModel * carModel;

@end

@implementation RulesListViewController
- (instancetype)initWithData:(NSArray *)dataArray initWithQueryID:(NSString *)queryIDStr withDataManager:(ViolationMgr *)cityMgr withCarModel:(ViolationCarModel *)carModel;
{
    self = [super init];
    if (self) {

        NSInteger fenNum = 0;
        NSInteger moneyNum = 0;
        
        for ( int index = 0; index < [dataArray count]; index ++) {
            ViolationModel * model = dataArray[index];
            fenNum += model.point;
            moneyNum += model.money;
        }
        NSInteger timeNum = dataArray.count;
        self.headArray = @[[NSString stringWithFormat:@"%zd",fenNum],[NSString stringWithFormat:@"%zd",moneyNum],[NSString stringWithFormat:@"%zd",timeNum]];
        self.dataArray = dataArray;
        
        self.queryIDStr = queryIDStr;
        
        self.cityMgr = cityMgr;
        
        self.carModel = carModel;
    }
    return self;
}
#pragma mark - 未完待续dealloc

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"违章查询列表";
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, __MAIN_HEIGHT_64) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [Theme backGroundColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.bounces = NO;
    self.tableView.separatorColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];

}
- (void)viewWillDisappear:(BOOL)animated
{
    [self.delegate configQueryIDStr:self.queryIDStr];
}
- (void)configDetailWithQueryID:(NSString *)queryID
{
    self.queryIDStr = queryID;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 100;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView * footerView = [Factory viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, 100) bgColor:[Theme backGroundColor] onView:nil];
    __weak __typeof(self) wself = self;
    [ViewFactory normalButtonWithFrame:FactoryRectButtonDefault(20) title:@"马上处理" inView:footerView action:^{
    
       [wself.cityMgr payLinkWithCarInfo:self.carModel.cxPayNeedInfos result:^(NSString *url) {
           [UmengMgr event:ICZ_CheckCarTraffic_PayService];
           BaseWebViewController * webVC = [[[BaseWebViewController alloc]initWithURL:url]setupNavBack];
           webVC.title = @"违章缴费";
           webVC.isScaleBool = YES;
           [wself.navigationController pushViewController:webVC animated:YES];
       }];
    }];
    return footerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CELL_SECTION_ONE + CELL_SECTION_TWO;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * headView = [Factory viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, CELL_SECTION_ONE + CELL_SECTION_TWO) bgColor:[UIColor clearColor] onView:nil];
    [Factory imageViewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, CELL_SECTION_ONE) image:ImageNamed(@"breakRules_car") onView:headView];
    UIView * labelView = [Factory viewWithFrame:CGRectMake(0,CELL_SECTION_ONE, __MAIN_WIDTH, CELL_SECTION_TWO) bgColor:COLOR_HEXSTRING(@"FFFFFF") onView:headView];
    CGFloat labelW = __MAIN_WIDTH / 3;
    NSArray * titleArray = @[@"扣分",@"罚款",@"未处理"];
    for (int index = 0 ; index < self.headArray.count; index ++ ) {
        CGFloat numLabelY = 17 * __MAIN_RATIO_H_667;
        CGFloat numLabelH = 22;
        UILabel * numLable = [Factory labelWithFrame:CGRectMake(labelW * index, numLabelY, labelW, numLabelH) font:FONT_DEFAULT_Light(25) text:[self.headArray objectAtIndex:index] textColor:[Theme mainColor] onView:labelView textAlignment:NSTextAlignmentCenter];
        numLable.tag = 10 + index;
        
        CGFloat textLabelY = numLabelY + numLabelH + 9 * __MAIN_RATIO_H_667;
        [Factory labelWithFrame:CGRectMake(labelW * index, textLabelY, labelW, numLabelH) font:FONT_DEFAULT_Light(14) text:[titleArray objectAtIndex:index] textColor:[Theme wordColor] onView:labelView textAlignment:NSTextAlignmentCenter];
    }
    
    return headView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [RulesListCell getCellHeight];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellID = @"cellID";
    RulesListCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[RulesListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    ViolationModel * model = self.dataArray[indexPath.row];
    [cell configWithData:model];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ViolationModel * model = self.dataArray[indexPath.row];
    NSDictionary * configDic = model.modelDic;
    RulesDetailViewController * ruleVC = [[RulesDetailViewController alloc]initWithData:configDic withQuryID:self.queryIDStr withDataManager:self.cityMgr withCarModel:self.carModel];
    ruleVC.delegate = self;
    [self.navigationController pushViewController:[ruleVC setupNavBack] animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
